﻿using QuickPasteItLib;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace QuickPasteIt.QuickPasteItVs
{
    class QuickPasteIt
    {
        private const string ERROR_MESSAGE = "QuickPasteIt Error: Invalid file specified";
        private const string PASTE_ERROR_MESSAGE = "QuickPasteIt Error: A problem occurred while communicating with the pastebin";
        private const string COPIED_MESSAGE = "Copied to clipboard: ";

        /// <summary>
        /// Pastebins the given text in the given language.
        /// </summary>
        public void PasteBin(string text, PasteLang lang)
        {
            try
            {
                // Get the pastebin to use -- fall back to user's preferred if none given
                QpiConfig qpiConfig = new QpiConfig();
                IPasteBin pastebin = QpiConfig.GetPasteBin(qpiConfig.PreferredPastebin);

                var submitResult = pastebin.SubmitPaste(text, lang, qpiConfig.Author);
                if (submitResult != "")
                {
                    Clipboard.SetText(submitResult);
                    MessageBox.Show(null, COPIED_MESSAGE + submitResult, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show(null, PASTE_ERROR_MESSAGE, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                MessageBox.Show(null, ERROR_MESSAGE, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Pastebins the given file.
        /// </summary>
        public void PasteBin(string filepath)
        {
            try
            {
                FileInfo givenFile = new FileInfo(filepath);
                Encoding fileEnc = Encoding.UTF8;
                QpiConfig qpiConfig = new QpiConfig();


                if (!givenFile.Exists || ((fileEnc = FileTools.DetectFileEncoding(givenFile)) == null && FileTools.FileHasNullBytes(givenFile)))
                {
                    MessageBox.Show(null, ERROR_MESSAGE, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    // Get the pastebin to use -- fall back to user's preferred if none given
                    IPasteBin pastebin = QpiConfig.GetPasteBin(qpiConfig.PreferredPastebin);

                    // Read file into string
                    string textToPaste = new StreamReader(givenFile.FullName, fileEnc).ReadToEnd();

                    var submitResult = pastebin.SubmitPaste(textToPaste, FileTools.GetPasteLangFromExtension(givenFile.Extension), qpiConfig.Author);
                    if (submitResult != "")
                    {
                        Clipboard.SetText(submitResult);
                        MessageBox.Show(null, COPIED_MESSAGE + submitResult, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    else
                    {
                        MessageBox.Show(null, PASTE_ERROR_MESSAGE, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
            catch
            {
                MessageBox.Show(null, ERROR_MESSAGE, "QuickPasteIt", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
